/**
 * 20182018年1月16日
 *
 * @author zhuwh4
 */
package com.utils.failureanalysis.data.util;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.utils.failureanalysis.data.model.singleModule.CountRecorder;
import com.utils.failureanalysis.data.model.singleRecorder.AnalysisData;
import jxl.*;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * @author zhuwh4
 *
 */
public class ExcelUtil {
    public static final String orderFile = "./modulesOrderRole.txt";
    private List<String> moduleOrder;
    BufferedReader br;

    public String creatExcel(List<AnalysisData> analysisDataList) {
		String xlsPath=FilePathUtil.filePathUtil.getResultExcelPath();
        try {
//			RGB 165,198,57 16进制#A5C639
            Color color1 = Color.decode("#A5C639");
//			RGB 212 233 169 16进制#D4E9A9
            Color color2 = Color.decode("#D4E9A9");
//			RGB 250 88 88 16进制#D4E9A9
            Color color3 = Color.decode("#FA5858");
//			线框
            Color color4 = Color.decode("#808080");
            Color color5 = Color.decode("#A5C639");

            WritableWorkbook book = Workbook.createWorkbook(new File(xlsPath));
            book.setColourRGB(Colour.BLUE, color1.getRed(), color1.getGreen(), color1.getBlue());
            book.setColourRGB(Colour.BLUE2, color2.getRed(), color2.getGreen(), color2.getBlue());
            book.setColourRGB(Colour.RED, color3.getRed(), color3.getGreen(), color3.getBlue());
            book.setColourRGB(Colour.GRAY_25, color4.getRed(), color4.getGreen(), color4.getBlue());
            book.setColourRGB(Colour.LIGHT_BLUE, color5.getRed(), color5.getGreen(), color5.getBlue());

            WritableSheet sheet = book.createSheet(getSheetName(), 0);
            WritableCellFormat wcf = new WritableCellFormat();
            wcf.setBackground(Colour.BLUE);
            wcf.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.GRAY_25);
            WritableCellFormat wcf2 = new WritableCellFormat();
            wcf2.setBackground(Colour.BLUE2);
            wcf2.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.LIGHT_BLUE);
            WritableCellFormat wcf3 = new WritableCellFormat();
            wcf3.setBackground(Colour.RED);
            wcf3.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.LIGHT_BLUE);

            Label labelA = new Label(0, 0, "模块名", wcf);
            Label labelB = new Label(1, 0, "测试时间", wcf);
            Label labelC = new Label(2, 0, "Test", wcf);
            Label labelD = new Label(3, 0, "Result", wcf);
            Label labelE = new Label(4, 0, "Details", wcf);
            Label labelF = new Label(5, 0, "StackTrece", wcf);
            Label labelG = new Label(6, 0, "fail原因", wcf);
            Label labelH = new Label(7, 0, "是否修改", wcf);
            Label labelI = new Label(8, 0, "修改思路", wcf);
            int i = 0;
            for (AnalysisData analysisData : analysisDataList) {
                i++;
                Label label1 = new Label(0, i, analysisData.getModuleName(), wcf2);
                Label label2 = new Label(1, i, analysisData.getDate(), wcf2);
                Label label3 = new Label(2, i, analysisData.getCaseName(), wcf2);
                Label label4 = new Label(3, i, analysisData.getPassOrFail(), wcf3);
                Label label5 = new Label(4, i, analysisData.getDetails(), wcf2);
                Label label6 = new Label(5, i, analysisData.getStackTrace(), wcf2);
                sheet.addCell(label1);
                sheet.addCell(label2);
                sheet.addCell(label3);
                sheet.addCell(label4);
                sheet.addCell(label5);
                sheet.addCell(label6);
            }
            sheet.addCell(labelA);
            sheet.addCell(labelB);
            sheet.addCell(labelC);
            sheet.addCell(labelD);
            sheet.addCell(labelE);
            sheet.addCell(labelF);
            sheet.addCell(labelG);
            sheet.addCell(labelH);
            sheet.addCell(labelI);
            book.write();
            book.close();
            System.out.println("-------Excel-failures 文件已创建-------");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return xlsPath;
    }

    public String creatExcelC(List<CountRecorder> l) {
        String xlsPath=FilePathUtil.filePathUtil.getCountResultExcelPath();
        readModuleOrder();
//		String xlsPath="";
        try {
            WritableWorkbook book = Workbook.createWorkbook(new File(xlsPath));
            WritableSheet sheet = book.createSheet(getSheetName(), 0);
            Label labelA = new Label(0, 0, "moduleName");
            Label labelB = new Label(1, 0, "pass");
            Label labelC = new Label(2, 0, "fail");
            int i = 0;
            int x;
//			确定module fail数统计排序顺序，优先读取排序规则文件来排序，否则按默认顺序排序
            if (moduleOrder != null && moduleOrder.size() > 0) {
                for (CountRecorder c : l) {
                    x = moduleOrder.indexOf(c.moduleName) + 1;
                    if (x == 0) {
                        i++;
                        x = moduleOrder.size() + i;
                    }
                    Label label1 = new Label(0, x, c.moduleName);
                    jxl.write.Number number2 = new Number(1, x, Integer.valueOf(c.passNum));
                    jxl.write.Number number3 = new Number(2, x, Integer.valueOf(c.failNum));
                    sheet.addCell(label1);
                    sheet.addCell(number2);
                    sheet.addCell(number3);
                }
            } else {
                for (CountRecorder c : l) {
                    i++;
                    Label label1 = new Label(0, i, c.moduleName);
                    jxl.write.Number number2 = new Number(1, i, Integer.valueOf(c.passNum));
                    jxl.write.Number number3 = new Number(2, i, Integer.valueOf(c.failNum));
                    sheet.addCell(label1);
                    sheet.addCell(number2);
                    sheet.addCell(number3);
                }
            }
            sheet.addCell(labelA);
            sheet.addCell(labelB);
            sheet.addCell(labelC);
            book.write();
            book.close();
            System.out.println("-------Excel-count pass and fail 文件已创建-------");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return xlsPath;
    }

    public String getSheetName() {
        SimpleDateFormat sf = new SimpleDateFormat("MMdd");
        String sheetName = sf.format(new Date()) + "fail分析";
        return sheetName;
    }

    /**
     * 读取排序规则文件
     */
    public void readModuleOrder() {
        moduleOrder = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(orderFile));
            String line;
            while ((line = br.readLine()) != null) {
                moduleOrder.add(line);
            }
        } catch (Throwable e) {
            System.out.println("未找到排序规则文件，按默认顺序排列");
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
